package cn.itrip.common;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisAPI {

    public JedisPool jedisPool;

    public JedisPool getJedisPool() {
        return jedisPool;
    }
    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    //get
    public String get(String key){
        Jedis jedis = jedisPool.getResource();
        String value = jedis.get(key);
        jedisPool.returnResource(jedis); //释放资源
        return value;
    }

    //set
    public String set(String key,String value){
        Jedis jedis = jedisPool.getResource();
        String set = jedis.set(key, value);
        jedisPool.returnResource(jedis);
        return set;
    }
    public String set(String key,int exp,String value){
        Jedis jedis = jedisPool.getResource();
        String result = jedis.setex(key,exp,value);
        jedisPool.returnResource(jedis);
        return result;
    }
    //exist
    public boolean exist(String key){
        Jedis jedis = jedisPool.getResource();
        Boolean exists = jedis.exists(key);
        jedisPool.returnResource(jedis);
        return exists;
    }
    //tll
    public Long ttl(String key){
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedisPool.returnResource(jedis);
        return result;
    }
    //del

    public Long del(String key){
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        jedisPool.returnResource(jedis);
        return result;
    }


}
